
-- Entity Designer DDL Script for SQL Server 2005, 2008, and Azure
-- --------------------------------------------------
-- Date Created: 05/18/2012 00:43:23
-- Generated from EDMX file: c:\users\richardgo\documents\visual studio 2010\Projects\T4DemoOne\T4Demo2\Model1.edmx
-- --------------------------------------------------

SET QUOTED_IDENTIFIER OFF;
GO
USE [t4DemoDB];
GO
IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]');
GO

-- Dropping existing FOREIGN KEY constraints


-- Dropping existing tables


-- --------------------------------------------------
-- Creating all tables
-- --------------------------------------------------

CREATE TABLE [dbo].[People] (
    [Id] int IDENTITY(1,1)NOT NULL,
    [Name] nvarchar(max) NOT NULL,
    [Phone] nvarchar(max) NOT NULL
);
GO

CREATE TABLE [dbo].[Products] (
    [Id] int IDENTITY(1,1)NOT NULL,
    [Name] nvarchar(max) NOT NULL,
    [SKU] nvarchar(max) NOT NULL
);
GO

CREATE TABLE [dbo].[Orders] (
    [Id] int IDENTITY(1,1)NOT NULL,
    [Date] datetime NOT NULL,
    [ProductId] int NOT NULL,
    [PersonId] int NOT NULL,
    [AddressId] int NOT NULL
);
GO

CREATE TABLE [dbo].[Addresses] (
    [Id] int IDENTITY(1,1)NOT NULL,
    [Line1] nvarchar(max) NOT NULL,
    [City] nvarchar(max) NOT NULL,
    [State] nvarchar(max) NOT NULL,
    [Zip] nvarchar(max) NOT NULL,
    [PersonId] int NOT NULL
);
GO

-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------

ALTER TABLE [dbo].[People]
ADD CONSTRAINT [PK_People]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

ALTER TABLE [dbo].[Products]
ADD CONSTRAINT [PK_Products]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

ALTER TABLE [dbo].[Orders]
ADD CONSTRAINT [PK_Orders]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

ALTER TABLE [dbo].[Addresses]
ADD CONSTRAINT [PK_Addresses]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------

ALTER TABLE [dbo].[Addresses]
ADD CONSTRAINT [FK_PersonAddress]
    FOREIGN KEY ([PersonId])
    REFERENCES [dbo].[People]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

CREATE INDEX [IX_FK_PersonAddress]
ON [dbo].[Addresses]
    ([PersonId]);
GO

ALTER TABLE [dbo].[Orders]
ADD CONSTRAINT [FK_OrderProduct]
    FOREIGN KEY ([ProductId])
    REFERENCES [dbo].[Products]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

CREATE INDEX [IX_FK_OrderProduct]
ON [dbo].[Orders]
    ([ProductId]);
GO

ALTER TABLE [dbo].[Orders]
ADD CONSTRAINT [FK_PersonOrder]
    FOREIGN KEY ([PersonId])
    REFERENCES [dbo].[People]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

CREATE INDEX [IX_FK_PersonOrder]
ON [dbo].[Orders]
    ([PersonId]);
GO

ALTER TABLE [dbo].[Orders]
ADD CONSTRAINT [FK_OrderAddress]
    FOREIGN KEY ([AddressId])
    REFERENCES [dbo].[Addresses]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

CREATE INDEX [IX_FK_OrderAddress]
ON [dbo].[Orders]
    ([AddressId]);
GO

-- --------------------------------------------------
-- Script has ended
-- --------------------------------------------------